package model.game;

import java.io.Serializable;
import java.util.ArrayList;
import model.game.scenario.Desert;
import model.game.scenario.Forest;
import model.game.scenario.JunyeCity;

public class Map implements Serializable{
    private ArrayList<Scenario> scenarios= new ArrayList<Scenario>();
    private ArrayList<Route> routes = new ArrayList<Route>();
    
    public Map(){
        addScenario(new JunyeCity());
        addScenario(new Forest());
        addScenario(new Desert());
        addRoute(new Route("Junye City", "Forest"));
    }

    public Place findPlace(String name){
        for (int i=0; i< scenarios.size(); i++){
            Object o = scenarios.get(i).findLocation(name);
            if (o !=null){      
                if (o instanceof Place){
                    Place p = (Place)o;
                    return p;
                }
            }
        }
        return null;
    }
    public String[] findNeighboors(Scenario s){
        String[] n= new String[50];
        int count = 0;
        for (int i=0; i<s.getPlaces().size(); i++){
            n[count] = s.getPlaces().get(i).getName();
            count++;
        }
        for (int i=0; i<scenarios.size(); i++){
            if (isNeighboor(s, scenarios.get(i))){
                n[count] = scenarios.get(i).getName();
                count++;
            }
        }
        return n;
    }
    public boolean playerExists(String name) {
       for (int i=0; i<scenarios.size(); i++){
           if (scenarios.get(i).playerExists(name)){
               return true;
           }
       }
       return false;
    } 
   public Player findPlayer(String name) {
       for (int i=0; i< scenarios.size(); i++){
           Player pl = scenarios.get(i).findPlayer(name);
           if (pl!=null){
               return pl;
           }
       }
       return null;
    }
   
   public Player findNPC(String name) {
       for (int i=0; i< scenarios.size(); i++){
           Player pl = scenarios.get(i).findNPC(name);
           if (pl!=null){
               return pl;
           }
       }
       return null;
    } 
   
   public void removePlayer(Player player){
       for (int i=0; i< scenarios.size(); i++){
           if (scenarios.get(i).playerExists(player.getName())){
               scenarios.get(i).removePlayer(player);
           }
       }
   }
   public void respawnPlayer(Player player){
       removePlayer(player);
       Location o = findLocation("Junye City");
       o.addPlayer(player);
   }
   public Location findLocation(String name){
       for (int i=0; i< scenarios.size(); i++){           
           Location o = scenarios.get(i).findLocation(name);
           if (o!=null){
               return o;
           }
       }
       return null;
   }
   public Scenario whatScenarioContainsPlace(String name){
       for (int i=0; i<scenarios.size(); i++){
           if (scenarios.get(i).findLocation(name)!=null){
               return scenarios.get(i);
           }
       }
       return null;
   }
   public Location whereIsPlayer(String name){
      for (int i=0; i< scenarios.size(); i++){
          Location o = scenarios.get(i).whereIsPlayer(name);
          if (o!=null){
              return o;
          }
       }
       return null;
   }
    public ArrayList<Scenario> getScenarios() {
        return scenarios;
    }

    public void setScenarios(ArrayList<Scenario> scenarios) {
        this.scenarios = scenarios;
    }
    public int getTotalPlayers(){
        int count = 0;
        for (int i=0; i< scenarios.size(); i++){
            count += scenarios.get(i).getTotalPlayers();
        }
        return count;
    }
   public void addPlayer(Player player, String location){
       removePlayer(player);
       Location o = findLocation(location);
       o.addPlayer(player);
   }
    public boolean isNeighboor(Location l1, Location l2){
        if (l1 instanceof Place){
            Place p = (Place)l1;
            Place p1 = findPlace(p.getName());
            if (p1 !=null){
                return true;
            }
            else{
                return false;
            }
        }
        else if (l2 instanceof Place){
            Place p = (Place)l2;
            Place p1 = findPlace(p.getName());
            if (p1 !=null){
                return true;
            }else{
                return false;
            }
        }
        Scenario s1 =(Scenario)l1;
        Scenario s2 = (Scenario)l2;
        for (int i=0; i<routes.size(); i++){
            if (routes.get(i).getA().equals(s1.getName())){
                if (routes.get(i).getB().equals(s2.getName())){
                    return true;
                }
            }else if (routes.get(i).getB().equals(s1.getName())){
                if (routes.get(i).getA().equals(s2.getName())){
                    return true;
                }
            }
        }
        return false;
    }
    
    public void addScenario(Scenario l){
        getScenarios().add(l);
    }
    public void addRoute(Route r){
        getRoutes().add(r);
    }

    public ArrayList<Route> getRoutes() {
        return routes;
    }
}
